<?php
namespace jy_storeping;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class team extends \plugin{
	/*
		核实分销商
	*/
	public function checkmember($mid=0,$isexi=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('team',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$teampage=$this->plugin_setting('teampage');
			$teampage['tagent']=$teampage['tagent']?:'团长';
			if(!$member['status']){
				$apply=$this->get('team',array('mid'=>$_SESSION['uid']),array('id','status'));
				if($apply['id'] && $apply['status']==0){
					if($isexi){
						exi('申请正在审核请耐心等待','error','jy_storeping/team.apply/');
					}else{
						return false;
					}
				}
				$member['can_apply']=false;
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->teamsetting['become']==0){
					//分销商无需审核，此处自动审核
					$member['can_apply']=true;
					$this->apply_agent($member['mid']);
					if($this->teamsetting['isshenhe'] && $isexi){
						exi('申请正在审核请耐心等待','error','jy_storeping/team.apply/');
					}else{
						exi('','','jy_storeping/team.index/');
					}
				}elseif($this->teamsetting['become']==3){
						//消费金额
						$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid),'price');
						$member['limit']=$this->teamsetting['oneorder_price'];
						$member['unit']='元';
						$member['desc']='消费满'.$member['limit'].'元';
						if($buyfee>=$this->teamsetting['oneorder_price']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
				}elseif($this->teamsetting['become']==2){
					$member['can_apply']=true;
					if($isexi){
						exi('你还不是'.$teampage['tagent'].'，请前往申请！','error','jy_storeping/team.apply/');
					}
				}elseif($this->teamsetting['become']==1){
					$member['desc']='暂时无法申请，只能由管理员后台指定';
				}
				if($this->teamsetting['isshenhe'] && $isexi && $ispass){
						exi('申请正在审核请耐心等待','error','jy_storeping/team.apply/');
				}elseif($ispass){
						$team=$this->get('team',array('mid'=>$_SESSION['uid']),array('id','status'));
						if($team['status']){
							exi('','','jy_storeping/team.index/');
						}
				}
				if($isexi){
						exi('你还不是'.$teampage['tagent'].'，请前往申请！','error','jy_storeping/team.apply/');
				}
			}elseif($member['stop'] && $isexi){
				exi('你的'.$teampage['tagent'].'身份已禁用！','error','jy_storeping/team.apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
                $getinfo=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
				if($this->teamsetting['isinfo']==1 && (!$getinfo['realname'] || !$getinfo['mobile'])){
					exi('请先完善信息','error','jy_storeping/team.info/');
				}else{
					
				}
			}
			return $member;
	}
	public function member_update($mid,$update=array()){
		pdo_update('core_members',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
		return true;
	}
	/*
		申请成为团队长
	*/
	public function apply_agent($mid,$area_code=''){
		$mid=$mid?:$_SESSION['uid'];
		$team=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($team['id'] && $team['status']==1){
			exi('您的申请已经通过！','error','jy_storeping/team.index/');
		}elseif($team['id'] && $team['status']==0){
			exi('您已经申请了团长！','error','jy_storeping/team.apply/');
		}
		if($team['id']){
			$new_apply=array(
				'status'=>0,
				'area_code'=>$area_code,
			);
			$this->update('team',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			$id=$team['id'];
		}else{
			$fromuser=pdo_getcolumn('jy_storeping_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>0,
				'area_code'=>$area_code,
				'fromuser'=>$fromuser?:$_SESSION['fromuser'],
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_storeping_team',$new_apply);
			$id=pdo_insertid();
		}
		if(!$this->teamsetting['isshenhe']){
			//分销商无需审核，此处自动审核
			$this->check_agent($id,1);
		}
		return $id;
	}
	/*
		审核团队长
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('team',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			$this->update_agent_level($agent_area['fromuser']);
		}else{
			$update['remark']=$remark;
		}
		$this->update('team',$update,array('id'=>$agent_id));
		return true;
	}
	public function set_parent($mid,$fromuser=0){
		$mid=$mid?:$_SESSION['uid'];
		if($mid==$fromuser || !$fromuser){
			return false;
		}
		$this->update('team',array('fromuser'=>$fromuser,'updatetime'=>TIMESTAMP),array('mid'=>$mid));
		$this->update_agent_level($fromuser);
		return true;
	}
	/*
		禁用团长
	*/
	public function stop_agent($agent_id){
		$this->update('team',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复团长
	*/
	public function start_agent($agent_id){
		$this->update('team',array('stop'=>0),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$self=false){
		//
		if(!$mid){
			return;
		}
		$agent=$this->get('team',array('mid'=>$mid));
		if(!$agent['mid']){
			return;
		}
		if($agent['status']!=1 && !$self){
			return;
		}
		$level=$this->get('teamlevel',array('id'=>$agent['level_id']));
		$level['level']=$level['level']?:0;
		$level_up_types=$this->model('model')->team_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$up=true;
		$levels=$this->getall('teamlevel',array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),'','','level desc');
		if(empty($levels)){
			return;
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				if($type=='myznum'){
					//自己拼中次数
					$agent['myznum']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'plugin_name'=>'jy_storeping','teamid >'=>0));
					if($agent['myznum']<$conditions['myznum']){
						$up=false;
						break;
					}
				}elseif($type=='myzfee'){
					//自己拼中金额
					$agent['myzfee']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'plugin_name'=>'jy_storeping','teamid >'=>0),'price');
					if($agent['myzfee']<$conditions['myzfee']){
						$up=false;
						break;
					}
				}elseif($type=='myorderfee'){
					//自己购买商品金额
					$agent['myorderfee']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'teamid'=>0),'price');
					if($agent['myorderfee']<$conditions['myorderfee']){
						$up=false;
						break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
				$update=array(
					'level_id'=>$next_level['id'],
					'status'=>1,
				);
				$this->update('team',$update,array('id'=>$agent['id']));
		}
		return true;
	}
	public function check_isgetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
			'status'=>3,
			'fee_type'=>$type,
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
		);
		$balance=$this->get('balance',$params);
		if($balance['id']){
			return true;
		}else{
			return false;
		}
	}
	//团队奖励入口
	public function add_commission($order=array(),$type=0){
		$this->isnum=0;
		$teamsetting=$this->plugin_setting('teamsetting');
		if($order['status']==10){
			//来自拼团的
			//return false;
		}
		$this->order=$order;		
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'storeid'=>$this->order['storeid'],
			'order_price'=>$this->order['price'],
			'commission_money'=>0,
			'jifen'=>0,
			'createtime'=>TIMESTAMP,
		);
		//现在支付方式分佣
		if($teamsetting['nopay_type']){
			$paylog=pdo_get('core_paylog',array('id'=>$order['pay_id']),array('pay_type','di_credit','di_fee','fee'));
			if($paylog['di_fee']>0){
				if(strpos($teamsetting['nopay_type'],';'.$paylog['di_credit'].';') !== false){
					$this->order['price'] -= $paylog['di_fee'];
				}
				if(strpos($teamsetting['nopay_type'],';'.$order['paytype'].';') !== false){
					$this->order['price'] -= $paylog['fee']-$paylog['di_fee'];
				}
				//两种支付都不支持
				if(strpos($teamsetting['nopay_type'],';'.$paylog['di_credit'].';') !== false && strpos($teamsetting['nopay_type'],';'.$order['paytype'].';') !== false){
					return;
				}
			}elseif(strpos($teamsetting['nopay_type'],';'.$order['paytype'].';') !== false){
				return;
			}
		}
		//1、默认方式: 除运费和会员折扣(或促销折扣)外其他所有费用全部计算分红；实际支付方式: 只计算实际支付和余额抵扣部分的分红(包括余额支付)
		if($teamsetting['fee_pricetype']!=2){
			$this->commission_order['commission_price']=$this->order['price'];
		}else{
			$this->commission_order['commission_price']=$this->order['price']-$this->order['dispatchprice'];
		}
		$member=$this->get('member',array('mid'=>intval($order['mid'])));
		$fromuser=$member['fromuser'];
		$this->commission_order['mypaimid']=$member['paimid'];
		$team=$this->get('team',array('mid'=>$fromuser));
		//直推单购奖励
		if($order['status']!=10 || $type!=2){
			$this->fee_commission($team,$type);
		}
		if($type==2){
			//排位奖励
			$paimid=$this->get_nowpaimid($member);
			$team=$this->get('team',array('mid'=>$paimid));
			if($team){
				$this->fee_commission($team,3);
			}
		}
		if($type==2){
			//生成分销订单
			$indata=$this->commission_order;
			unset($indata['isarea']);
			unset($indata['maxlevel']);
			unset($indata['red_money']);
			unset($indata['mypaimid']);
			$this->insert('fee_order',$indata);
		}
		return true;
	}
	public function fee_commission($team=array(),$type=0){
		$this->isnum ++;
		if($this->isnum>20){
			return false;
		}
		$teamsetting=$this->plugin_setting('teamsetting');
		if(!$teamsetting['isopen']){
			return false;
		}
		$teamsetting['team_ceng']=$teamsetting['team_ceng']?:6;
		//排
		if($type==3){
				$pailog=array(
					'mid'=>$this->order['mid'],
					'paimid'=>$this->commission_order['mypaimid'],
					'lasttomid'=>$team['mid'],
				);
				$nowceng=$this->set_pailog($pailog);
				if(!$nowceng){
					$nowteam=$this->get('team',array('mid'=>$this->commission_order['mypaimid']));
					if($nowteam && $nowteam['mid']!=$team['mid']){
						$this->fee_commission($nowteam,3);
						return false;
					}
				}
				$ceng=$this->get_myceng($nowteam);
				if($ceng<=$nowceng){
					if($nowceng<$teamsetting['team_ceng']){
						$nowteam=$this->get('team',array('mid'=>$team['paimid']));
						if($nowteam && $nowteam['mid']!=$team['mid']){
							$this->fee_commission($nowteam,3);
						}else{
							$nowteam=$this->get('team',array('mid'=>$this->commission_order['mypaimid']));
							if($nowteam && $nowteam['mid']!=$team['mid']){
								$this->fee_commission($nowteam,3);
							}
						}
					}
					return false;
				}
		}
		$level=pdo_get('jy_storeping_teamlevel',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['level_id']));
		$commissions=$level['commissions']?json_decode($level['commissions'],true):array();
		$money=0;
		$jifen=0;
		$jifenper=0;
        $total_price=$this->order['price'];
		if(!$type){
			//直推拼中
			$moneyper=$commissions['zp_per'];
		}elseif($type==1){
			//直推未拼中
			$moneyper=$commissions['zwp_per'];
		}elseif($type==2){
			//直推单购
			$moneyper=$commissions['zdp_per'];
		}elseif($type==3){
			//团队排位奖
			$moneyper=$commissions['team_per'];
			$jifenper=$commissions['team_biper'];
		}
		$order_price=$this->commission_order['commission_price'];
		$this->needorder=false;
		if(!$this->order['goods']){
			$this->order['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$this->order['id']),array('goodsid','total_price','total','price','optionid'));
		}
		foreach($this->order['goods'] as $goods){
				$goodsid=$this->getcolumn('goods',array('goodsid'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'id');
				$ignore=$this->get('fee_goods',array('goods_id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
				if($ignore['ignore']  || !$ignore['id']){
					$goods_commission=array();
					if($ignore['ignore']){
						$order_price=$order_price-$total_price;
					}
					$this->needorder=true;
					continue;
				}else{
					$goods_commission=$ignore['commissions']?json_decode($ignore['commissions'],true):array();
				}
				$order_price=$order_price-$goods['total_price'];
				foreach($goods_commission as $gc){
						if($gc['level_id']!=$team['level_id']){
							continue;
						}else{
							if(!$type){
								//直推拼中
								$goods_moneyper=$gc['zp_per'];
							}elseif($type==1){
								//直推未拼中
								$goods_moneyper=$gc['zwp_per'];
							}elseif($type==2){
								//直推单购
								$goods_moneyper=$gc['zdp_per'];
							}elseif($type==3){
								//团队排位奖
								$goods_moneyper=$gc['team_per'];
								$goods_jifenper=$gc['team_biper'];
							}
							break;
						}
				}
				if(substr($goods_moneyper,-1)=='%'){
					$goods_money=intval($goods['total_price']*substr($goods_moneyper,0,-1))/100;
					$money+=$goods_money;
				}else{
					$goods_money=intval($goods_moneyper*$goods['total']*100)/100;
					$money+=$goods_money;
				}
				//积分
				if(substr($goods_jifenper,-1)=='%'){
					$goods_jifen=intval($goods['total_price']*substr($goods_jifenper,0,-1))/100;
					$jifen+=$goods_jifen;
				}else{
					$goods_jifen=intval($goods_jifenper*$goods['total']*100)/100;
					$jifen+=$goods_jifen;
				}
				$remark['goods_commission'][]=array('goods_id'=>$goods['goodsid'],'total_price'=>$goods['total_price'],'total'=>$goods['total'],'commission_per'=>$goods_moneyper,'commission_money'=>$goods_money);
		}
		if($order_price>0 && $this->needorder){
				if(substr($moneyper,-1)=='%'){
					$order_commission_money=intval($order_price*substr($moneyper,0,-1))/100;
				}else{
					$order_commission_money=$moneyper;
				}
				$money+=$order_commission_money;
				//积分
				if(substr($jifenper,-1)=='%'){
					$demo_jifen=intval($order_price*substr($jifenper,0,-1))/100;
				}else{
					$demo_jifen=$jifenper;
				}
				$jifen+=$demo_jifen;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$moneyper,'commission_money'=>$order_commission_money);
		}
		if($money>0 || $jifen>0){
			$this->commission_order['commission_money'] +=$money;
			$this->commission_order['jifen'] +=$jifen;
			if($type==3){
				$rate=$this->get_myteamsfrate($team['mid']);
				$newmoney=$rate*$money/100;
				$fee5=$money-$newmoney;
				if($fee5>0){
					$money=$newmoney;
					model('core/member')->member_credit($team['mid'],'credit5',$fee5,true,'jy_storeping','按比例释放排位奖购物积分');
				}
			}
			if($teamsetting['fee_totype']==1){
				if($teamsetting['fee_tobi5']){
					$jifen5=$teamsetting['fee_tobi5']*$money/100;
				}
				if($teamsetting['fee_tofee']){
					$money=$teamsetting['fee_tofee']*$money/100;
				}
			}elseif($teamsetting['fee_totype']==2){
				if($teamsetting['fee_tobi5']){
					$jifen5=$teamsetting['fee_tobi5']*$money/100;
				}
				if($teamsetting['fee_tobi1']){
					$jifen=$teamsetting['fee_tobi1']*$money/100;
				}
			}
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$team['mid'],
				'orderid'=>$this->order['id'],
				'money'=>$money,
				'jifen'=>$jifen,
				'jifen5'=>$jifen5,
				'remark'=>ijson_encode($remark),
				'createtime'=>TIMESTAMP,
				'status'=>$teamsetting['isbuy_jiesuan']?1:0,
				'type'=>$type,
			);
			$this->insert('commission',$new_commission);
		}else{
			if($type==3 && $team['paimid']){
				$team=$this->get('team',array('mid'=>$team['paimid']));
				if($team && $team['paimid']!=$team['mid']){
					$this->fee_commission($team,3);
				}
			}
		}
		return true;
	}
	public function getteamids($teammid){
		$newmembers=array();
		$newteammids=$teammid;
		for($i=1;$i<12;$i++){
			$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$newteammids),array('mid'),'mid');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=array();
		}
		return $teamids;
	}
	//获取当前循环到哪个坑位
	public function get_nowpaimid($member=array()){
		$teamsetting=$this->plugin_setting('teamsetting');
		$level=intval($teamsetting['team_ceng']);
		$level=$level?:6;
		$pailog=pdo_get('jy_storeping_pailog',array('paimid'=>$member['paimid'],'uniacid'=>$_SESSION['uniacid']));
		if($pailog['lasttomid'] && $pailog['ceng']<$level){
			$paimid=pdo_getcolumn('jy_storeping_member',array('mid'=>$pailog['lasttomid'],'uniacid'=>$_SESSION['uniacid']),'paimid');
		}
		if($pailog['ceng']>$level || ($pailog['id'] && !$paimid)){
			pdo_update('jy_storeping_pailog',array('ceng'=>0,'lasttomid'=>0),array('id'=>$pailog['id'],'uniacid'=>$_SESSION['uniacid']));
			return $pailog['paimid'];
		}
		$paimid=$paimid?:$member['paimid'];
		return $paimid;
	}
	//直推1人消费1次升级多拿1层
	public function get_myceng($team=array()){
		$teamsetting=$this->plugin_setting('teamsetting');
		if($teamsetting['team_tuitime']<=0){
			return 1;
		}
		$mids=pdo_getall('jy_storeping_member',array('fromuser'=>$team['mid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		$buynum=0;
		if($mids){
			$mids=array_keys($mids);
			$buynum=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$mids,'uniacid'=>$_SESSION['uniacid']),'id');
		}	
		$addtime=$buynum/$teamsetting['team_tuitime'];
		$teamsetting['team_ceng']=$teamsetting['team_ceng']?:6;
		$ceng=$addtime>$teamsetting['team_ceng']?$teamsetting['team_ceng']:$addtime;
		return $ceng;
	}
	//消费1次获得***元解冻团队层级循环奖几分之一为1个额度释放值；
	public function get_myteamsf($member=array()){
		$teamsetting=$this->plugin_setting('teamsetting');
		$team_sftime=$teamsetting['team_sftime']?:1;
		$teamsf=1/$team_sftime;
		pdo_update('jy_storeping_member',array('teamsf +='=>$teamsf),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
		$params=array(
				'storeid'=>$this->order['storeid'],
				'orderid'=>$this->order['id'],
				'fee'=>$teamsf,
				'msg'=>'消费获得排位奖释放额！',
				'status'=>1,
				'mid'=>$member['mid'],
		);
		$this->model('model')->set_feelog($params,6);
		return true;
	}
	//获取释放比例
	public function get_myteamsfrate($mid=0){
		$member=pdo_get('jy_storeping_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$yueday=date('Y-M');
		$yuetime=strtotime($yueday.'-01 00:00:00');
		if($member['createtime']>$yuetime){
			return 100;
		}
		$teamsetting=$this->plugin_setting('teamsetting');
		$teamsetting['team_ceng']=$teamsetting['team_ceng']?:6;
		if($member['teamsf']>$teamsetting['team_ceng']){
			return 100;
		}elseif($member['teamsf']<=0){
			$teamsfrate=0;
		}else{
			$teamsfrate=$member['teamsf']*100/$teamsetting['team_ceng'];
		}
		return $teamsfrate;
	}
	//每月扣除
	public function get_kouteamsf($mid=0){
		$member=pdo_get('jy_storeping_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$syuetime=mktime(0, 0 , 0,date("m")-2,1,date("Y"));
		if($member['createtime']>$syuetime){
			return false;
		}
		$yueday=date('Y-M');
		$yuetime=strtotime($yueday.'-01 00:00:00');
		$feelog=pdo_get('jy_storeping_feelog',array('createtime >'=>$yuetime,'type'=>7,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($feelog['id']){
			return false;
		}
		$teamsetting=$this->plugin_setting('teamsetting');
		$teamsetting['team_ceng']=$teamsetting['team_ceng']?:6;
		if($member['teamsf']>$teamsetting['team_ceng']){
			$koufee=$teamsetting['team_ceng'];
		}else{
			$koufee=$member['teamsf'];
		}
		$params=array(
				'fee'=>$koufee,
				'msg'=>'每月扣除排位奖释放额！',
				'status'=>1,
				'mid'=>$member['mid'],
		);
		$this->model('model')->set_feelog($params,7);
		return true;
	}
	/*
		排坑
	*/
	public function set_pailog($params){
		$teamsetting=$this->plugin_setting('teamsetting');
		$pailog=pdo_get('jy_storeping_pailog',array('paimid'=>$params['paimid'],'uniacid'=>$_SESSION['uniacid']));
		if($pailog['id']){
			$level=intval($teamsetting['team_ceng']);
			$level=$level?:6;
			if($pailog['ceng']>$level || !$params['lasttomid']){
				pdo_update('jy_storeping_pailog',array('ceng'=>1,'lasttomid'=>$pailog['paimid']),array('id'=>$pailog['id'],'uniacid'=>$_SESSION['uniacid']));
				return 0;
			}else{
				pdo_update('jy_storeping_pailog',array('ceng +='=>1,'lasttomid'=>$params['lasttomid']),array('id'=>$pailog['id'],'uniacid'=>$_SESSION['uniacid']));
				$ceng=$pailog['ceng']+1;
			}
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$params['mid']?:$_SESSION['uid'],
				'paimid'=>$params['paimid'],
				'ceng'=>1,
				'lasttomid'=>$params['lasttomid'],
				'createtime'=>TIMESTAMP
			);
			$ceng=1;
			pdo_insert('jy_storeping_pailog',$insert);
		}
		return $ceng;
	}
}